import axios from "axios";
import qs from "qs";
// import store from "@/store/index";
// import { Loading } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

const serverURL = [
	"http://192.168.104.240:8282/",  //公司内网
	"http://218.75.111.145:8282/",  //天演公网
	'http://10.171.142.66:8282/',   //政务云
	"https://nh.hzbiz.net/",  //天演公网
	"http://192.168.104.217:8280/" //公司内网
]

const service = axios.create({
	baseURL: serverURL[3],
	headers: { 
		post: {
			"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
		},
	},
	timeout: 2000000,
});


// 添加请求拦截器
// eslint-disable-next-line no-unused-vars
// let loadingservice;

// let message = function (error) {
//     Message({
//         message: error,
//         type: 'warning',
//         duration: 3000
//     })
// }

// let success = function (res) {
//     Message({
//         message: res,
//         type: 'success',
//         duration: 3000
//     })
// }



//这里是取消重复请求的函数
const pending = new Map();
/**
 * 添加请求
 * @param {Object} config
 */
const addPending = config => {
	const url = [config.method, config.url, qs.stringify(config.params), qs.stringify(config.data)].join("&");
	config.cancelToken =
		config.cancelToken ||
		new axios.CancelToken(cancel => {
			if (!pending.has(url)) {
				// 如果 pending 中不存在当前请求，则添加进去
				pending.set(url, cancel);
			}
		});
};
/**
 * 移除请求
 * @param {Object} config
 */
const removePending = config => {
	const url = [config.method, config.url, qs.stringify(config.params), qs.stringify(config.data)].join("&");
	if (pending.has(url)) {
		// 如果在 pending 中存在当前请求标识，需要取消当前请求，并且移除
		const cancel = pending.get(url);
		cancel(url);
		pending.delete(url);
	}
};

service.interceptors.request.use(
	config => {
		// console.log(config)
		// Do something before request is sent
		removePending(config); // 在请求开始前，对之前的请求做检查取消操作
		addPending(config); // 将当前请求添加到 pending 中
		//再url后面加上当前时间戳（需要的话）

		if (config.url.indexOf("?") !== -1) {
			config.url += `&t=${new Date().getTime()}`;
		} else {
			config.url += `?t=${new Date().getTime()}`;
		}
		//对传入数据进行格式转化
		config.transformRequest = [
			data => {
				if (data instanceof FormData) {
					return data;
				} else {
					return qs.stringify(data, {
						allowDots: true,
					});
				}
			},
		];
		//项目中的token，名称不匹配后期修改
		let token;
		if (localStorage.getItem('userInfo')) {
			token = JSON.parse(localStorage.getItem('userInfo')).token
			if (typeof token != 'undefined') {
				config.headers.Authorization = `Bearer ${token}`;
			}
		}
		let SYS_TYPE;
		if (localStorage.getItem('SYS_TYPE')) {
			SYS_TYPE = localStorage.getItem('SYS_TYPE')
			if (typeof SYS_TYPE != 'undefined') {
				config.headers.SYS_TYPE = localStorage.getItem('SYS_TYPE');
			}
		}else{
			config.headers.SYS_TYPE = "SS1";
		}
		
		// loadingservice = Loading.service({
		// 	text:"正在加载数据，请稍等",
		// 	lock: true,
		// 	spinner: 'el-icon-loading',
		// 	background: 'rgba(0, 0, 0, 0.7)'
		// })
		// // loadingservice.close()
		return config;
	},
	error => {
		// loadingservice.close()
		return Promise.reject(error);
	}
);

service.interceptors.response.use(
	response => {
		removePending(response);
		const res = response.data;
			// loadingservice.close()
		return res;
	},
	error => {
			// loadingservice.close()
		return Promise.reject(error);
	}
);

export default service;
